package com.wanxi.dangdang.controller.front.order;

import com.github.pagehelper.PageInfo;
import com.wanxi.dangdang.common.CommonResult;
import com.wanxi.dangdang.common.OrderStateCode;
import com.wanxi.dangdang.model.dto.COrderByIdDto;
import com.wanxi.dangdang.model.dto.OrderAddDto;
import com.wanxi.dangdang.model.pojo.OrderDetails;
import com.wanxi.dangdang.model.vo.*;
import com.wanxi.dangdang.service.OrderService;
import com.wanxi.dangdang.util.JwtTokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: 寇文宝
 * @Description: 用户c端用户订单处理
 * @Date: Created in 2021/8/30
 * @Version: 1.0
 */

@Api(tags = "C端用户订单管理，均需要登录")
@RestController
@RequestMapping("/front/c/order")
public class COrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @ApiOperation("用户通过微信id查询自己在当前门店的所有订单，传入门店id")
    @GetMapping("/findByStoreIdAndWeChatId")
    public CommonResult findByStoreIdAndWeChatId(CUserAndStoreOrderVo cUserAndStoreOrderVo, HttpSession session) {
        Long id = (Long) session.getAttribute("id");
        cUserAndStoreOrderVo.setWeChatId(id + "");
        PageInfo byStoreIdAndWeChatId = orderService.findByStoreIdAndWeChatId(cUserAndStoreOrderVo);
        return CommonResult.success(byStoreIdAndWeChatId);
    }

    @ApiOperation("用户查询自己当前门店内的不同状态的订单，传入门店id和订单状态")
    @GetMapping("/findByStoreIdAndWeChatIdAndStatus")
    public CommonResult findByStoreIdAndWeChatIdAndStatus(CUserOrderVo cUserOrderVo, HttpSession session) {
        Long id = (Long) session.getAttribute("id");
        cUserOrderVo.setWeChatId(id + "");
        PageInfo byStoreIdAndWeChatIdAndStatus = orderService.findByStoreIdAndWeChatIdAndStatus(cUserOrderVo);
        return CommonResult.success(byStoreIdAndWeChatIdAndStatus);
    }

    @ApiOperation("用户查询自己的订单详情（根据订单id）")
    @GetMapping("/findById")
    public CommonResult findById(Long id, HttpSession session) {
        Long id1 = (Long) session.getAttribute("id");
        String weChatId = id1 + "";
        OrderPo byId = orderService.findCOrderById(id, weChatId);
        COrderByIdDto cOrderByIdDto = new COrderByIdDto();
        BeanUtils.copyProperties(byId,cOrderByIdDto);
        cOrderByIdDto.setOrderState(OrderStateCode.getOrderStateCodeMsg(byId.getOrderState()));
        List<OrderDetails> orderDetailsList = byId.getOrderDetailsList();
        List<OrderDetailsVo> orderDetailsVoList = new LinkedList<>();
        for (OrderDetails orderDetails : orderDetailsList) {
            OrderDetailsVo orderDetailsVo = new OrderDetailsVo();
            BeanUtils.copyProperties(orderDetails, orderDetailsVo);
            orderDetailsVo.setTicketType(orderDetails.getTicketType().equals("0") ?"儿童票":"成人票");
            orderDetailsVoList.add(orderDetailsVo);
        }
        cOrderByIdDto.setOrderDetailsVoList(orderDetailsVoList);
        return CommonResult.success(cOrderByIdDto);
    }

    @ApiOperation("用户申请退款，传入订单id和退款原因")
    @PostMapping("/refund")
    @ApiParam(name = "refundVo", value = "用户退款实体类")
    public CommonResult refund(@RequestBody RefundVo refundVo, HttpSession session) {
        Long id = (Long) session.getAttribute("id");
        refundVo.setWeChatId(id + "");
        String refund = orderService.refund(refundVo);
        return CommonResult.success(refund);
    }

    @ApiOperation("用户申请取消订单，传入订单id")
    @PostMapping("/cancelOrder")
    @ApiParam(name = "cUserOrderVo", value = "c端用户订单vo类")
    public CommonResult cancelOrder(@RequestBody CancelOrderVo cancelOrderVo, HttpSession session) {
        Long id = (Long) session.getAttribute("id");
        cancelOrderVo.setWeChatId(id + "");
        String cancelOrder = orderService.cancelOrder(cancelOrderVo);
        return CommonResult.success(cancelOrder);
    }

    @ApiOperation("根据用户提交信息生成订单,先生成订单表，再把主键回填，然后生成订单详情表")
    @PostMapping("/add")
    @ApiParam(name = "orderVo", value = "生成订单实体类")
    public CommonResult add(@RequestBody OrderVo orderVo, HttpSession session) {
        String attribute = request.getHeader("Authorization");
        String authToken = attribute.substring("bearer".length()).trim();
        Long idFromToken = jwtTokenUtil.getIdFromToken(authToken);


        Long id = (Long) session.getAttribute("id");
//        Long id = 7L;
        if (id == null){
            return CommonResult.failed("未登录，请登录后再操作");
        }
        orderVo.setWeChatId(id + "");
        OrderPo orderPo = new OrderPo();
        BeanUtils.copyProperties(orderVo, orderPo);
        List<OrderDetailsVo> orderDetailsVoList = orderVo.getOrderDetailsVoList();
        List<OrderDetails> orderDetailsList = new LinkedList<>();
        for (OrderDetailsVo orderDetailsVo : orderDetailsVoList) {
            OrderDetails orderDetails = new OrderDetails();
            BeanUtils.copyProperties(orderDetailsVo, orderDetails);
            orderDetails.setCreateTime(new Date());
            orderDetails.setUpdateTime(new Date());
            orderDetailsList.add(orderDetails);
        }
        orderPo.setOrderDetailsList(orderDetailsList);
        String add = orderService.add(orderPo);
        //支付成功后跳转支付页面返回的数据
        OrderPo byId = orderService.findById(orderPo.getId());
        OrderAddDto orderAddDto = new OrderAddDto();
        BeanUtils.copyProperties(byId,orderAddDto);
        return CommonResult.success(orderAddDto,add);
    }

    @ApiOperation("支付API")
    @PostMapping("/payMoney")
    @ApiParam(name = "id", value = "订单ID")
    public CommonResult payMoney(@RequestBody PayMoneyVo payMoneyVo) {
        Long id = payMoneyVo.getId();
        String payMoney = orderService.payMoney(id);
        return CommonResult.success(payMoney);
    }
}
